//
//  CMDBDiscoverViewController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/25.
//  Copyright © 2017年 CMDB. All rights reserved.
//CMDBHomeShowLogoViewController

import UIKit

private let sortBtnCount = 3

private let sortBtnWidth = CMDB_Screen_Width / CGFloat(sortBtnCount)

class CMDBDiscoverViewController:  CustomNormalContentViewController{
    
    var filterController : CMDBSliderRightViewController?
    
    var headerView : UIView!
    
    var selectedBtn : CMDBSearchCategoryBtn?
    
    lazy var searchBar : UISearchBar = {
        let searchBar = UISearchBar()
        
        searchBar.delegate        = self
        
        searchBar.placeholder     = "请输入您要搜索的选项"
        
        searchBar.tintColor       = UIColor.whiteColor()
        
        let searchBg = self.getImageWithColor(UIColor.whiteColor(), height: 44)
        
        searchBar.backgroundColor = UIColor.lightGrayColor()
        
        searchBar.searchFieldBackgroundImageForState(.Normal)
        
        let searchField = searchBar.valueForKey("_searchField") as! UITextField
        
        searchField.tintColor = UIColor.blueColor()
    
        let lineView = UIView.CreateLine(CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: 0.5), lineColor: word12Color)
        
        searchField.addSubview(lineView)
        
        lineView.snp_makeConstraints { (make) in
            make.left.equalTo(0);
            make.right.equalTo(0);
            make.bottom.equalTo(1);
            make.height.equalTo(0.5)
        }
        
        let magnifier = UIImage(named: "搜索－灰")
        
        let iconView = UIImageView.init(image: magnifier)
        
        iconView.frame = CGRect(x: 0, y: 0, width: magnifier!.size.width, height: magnifier!.size.height)
        
        searchField.leftView      = iconView
        
        searchField.leftViewMode  = .Always
        
        searchBar.backgroundImage = searchBg
        
        return searchBar
        
    }()
    
    var resultPeopleTableView : CMDBResultPeopleTableView!
    
    var resultMovieTableView : CMDBResultMovieTableView!

    override func setup() {
        super.setup()
        
        self.filterController = CMDBSliderRightViewController.init(sponsor: self, commitBlock: { (commitArray) in
            
            }, resetBlock: { (restArray) in
                
        })
    }
    
    override func buildTitleView() {
        super.buildTitleView()
        
        let iconView       = UIImageView(image: UIImage(named: "cmdb"))
        
        let middleY        = (titleView?.middleY)! + 8
        
        iconView.center    = CGPoint(x: (titleView?.middleX)!, y: middleY)
        
        titleView?.addSubview(iconView)
        
        let button                    = UIButton(frame : CGRect(x: CMDB_Screen_Width - 50, y: 0, width: 100, height: 64))
        button.center                 = CGPoint(x: CMDB_Screen_Width - 20, y: self.titleView!.middleY + 10)
        button.imageView?.contentMode = .Center
        button.setImage(UIImage(named: "筛选"),     forState: .Normal)
        button.setImage(UIImage(named: "筛选"), forState: .Highlighted)
        
        self.titleView?.addSubview(button)
        
        button.addTarget(self, action: #selector(show), forControlEvents: .TouchUpInside)
    
    }
    
    override func buildContentView() {
        super.buildContentView()
        
        buildHeaderSearchView()
        
        buildTableView()
        
    }

}

extension CMDBDiscoverViewController{
    
    func buildHeaderSearchView(){
        
        headerView = UIView.getInstance()
        
        headerView.frame = CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: 145 + 45 + 15)
        
        searchBar.frame = CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: 44)
        
        headerView.addSubview(searchBar)
        
        contentView?.addSubview(headerView)
        
        let peopleBtn = CMDBSearchCategoryBtn(titele: "影人", textColor: UIColor.Hex6(0x000000, alpha: 0.54), selectedTextColor: UIColor.Hex6(0xff6d00, alpha: 0.87), image: "影人－未选中", selectedImage: "影人－选中", titleSizeFont: CMDB_15_FONT)
        
        peopleBtn.selected = true
        
        peopleBtn.userInteractionEnabled = false
        
        peopleBtn.tag = 0
        
        selectedBtn = peopleBtn
        
        peopleBtn.addTarget(self, action: #selector(click(_:)), forControlEvents: .TouchUpInside)
        
        peopleBtn.frame = CGRect(x: 110, y: 69, width: 35, height: 50)
        
        let movieBtn    = CMDBSearchCategoryBtn(titele: "作品", textColor: UIColor.Hex6(0x000000, alpha: 0.54), selectedTextColor: UIColor.Hex6(0xff6d00, alpha: 0.87), image: "作品－未选中", selectedImage: "作品－选中", titleSizeFont: CMDB_15_FONT)
        
        movieBtn.addTarget(self, action: #selector(click(_:)), forControlEvents: .TouchUpInside)
        
        movieBtn.tag = 1
        
        headerView.addSubview(movieBtn)
        
        movieBtn.snp_makeConstraints { (make) in
            make.right.equalTo( -110);
            make.height.equalTo(50);
            make.width.equalTo(35);
            make.top.equalTo(69);
            
        }
        
        headerView.addSubview(peopleBtn)
        
        let singleView = UIView.CreateLine(CGRect(x: 0, y: 148, width: CMDB_Screen_Width, height: 15), lineColor: UIColor.Hex(0xf2f2f2))
        
        headerView.addSubview(singleView)
        
        buildSortOrder()
        
    }
    
    
    func buildSortOrder(){
        
        let titleArr = ["作品数量","年龄","入行年数"]
        
        let sortView = UIView.CreateLine(CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: 43), lineColor: UIColor.whiteColor())
        
        headerView.addSubview(sortView)
        
        sortView.snp_makeConstraints { (make) in
            make.left.equalTo(0);
            make.right.equalTo(0);
            make.bottom.equalTo(0);
            make.height.equalTo(43);
        }

        for index in 0 ..< sortBtnCount {
            
            let sortBtn = CMDBFilterSortBtn(titele: titleArr[index], textColor: word87Color, image: "降序－点击", selectedImage: "升序－点击", titleSizeFont: CMDB_15_FONT)
            
            sortBtn.frame = CGRect(x: CGFloat(index) * sortBtnWidth, y: 0, width: sortBtnWidth , height: 44)
            
            sortView.addSubview(sortBtn)
        }
        
        sortView.addSubview(UIView.CreateLine( CGRect(x: 0, y: 43.5, width: CMDB_Screen_Width, height: 0.5), lineColor: word38Color))
    }
    
    func buildTableView(){
        
        let contentH = CMDB_Screen_Height - 205 - 64 - 49
        
        let contentFrame = CGRect(x: 0, y: 205, width: CMDB_Screen_Width, height: contentH)
        
        resultPeopleTableView                  = CMDBResultPeopleTableView(frame: contentFrame)
        resultPeopleTableView.separatorStyle               = .None
        resultPeopleTableView.showsVerticalScrollIndicator = false
        resultPeopleTableView.mDelegate                    = self
        resultPeopleTableView.backgroundColor              = UIColor.Hex6(0x000000, alpha: 0.12)
        
        contentView!.addSubview(resultPeopleTableView)
        
        
        resultPeopleTableView.hidden = false
        
        resultPeopleTableView.reloadData()
        
        buildMovieTableView()
    }
    
    func buildMovieTableView(){
        let contentH = CMDB_Screen_Height - 205 - 64 - 49
        
        let contentFrame = CGRect(x: 0, y: 205, width: CMDB_Screen_Width, height: contentH)
        
        resultMovieTableView                  = CMDBResultMovieTableView(frame: contentFrame)
        resultMovieTableView.separatorStyle               = .None
        resultMovieTableView.showsVerticalScrollIndicator = false
        resultMovieTableView.mDelegate                    = self
        resultMovieTableView.backgroundColor              = UIColor.Hex6(0x000000, alpha: 0.12)
        
        contentView!.addSubview(resultMovieTableView)

        resultMovieTableView.hidden = true
    }
}

extension CMDBDiscoverViewController : UISearchBarDelegate{
    
    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        searchBarBecomeFirstResponder()
    }

    func searchBarTextDidEndEditing(searchBar: UISearchBar) {
        searchBarResignFirstResponder()
    }
}


extension CMDBDiscoverViewController{
    
    @objc func show(){
        
        self.filterController?.show()
        
    }
    
    // MARK: - 事件
    @objc private func searchBarBecomeFirstResponder() {
        searchBar.becomeFirstResponder()
//        resetBarBtnSate()
    }
    
    @objc private func searchBarResignFirstResponder() {
//        searchBar.resignFirstResponder()
//        resetBarBtnSate()
    }
    
    func resetBarBtnSate(){
        
        if searchBar.isFirstResponder() {
            //弹出键盘
            
        }
        
    }
}

//MARK: - action method
extension CMDBDiscoverViewController{
    
    @objc func click(sender : CMDBSearchCategoryBtn){
        
        sender.selected = !sender.selected
        
        sender.userInteractionEnabled = !sender.userInteractionEnabled
        
        if sender.tag != selectedBtn?.tag {
            if sender.tag == 0 {
                resultMovieTableView.hidden = true
                resultPeopleTableView.hidden = false
            }else{
                resultPeopleTableView.hidden = true
                resultMovieTableView.hidden = false

            }

        }
        
        selectedBtn?.selected = !(selectedBtn?.selected)!
        
        selectedBtn?.userInteractionEnabled = !(selectedBtn?.userInteractionEnabled)!
        
        selectedBtn = sender
        
    }
    
}

extension CMDBDiscoverViewController{
    
    override func onItemClickType(itemClickType: String?, data: AnyObject?, indexPath: NSIndexPath?) {
        self.navigationController?.pushViewController(CMDBTestViewController(), animated: true)
    }
    
}


extension CMDBDiscoverViewController{
    
    func getImageWithColor(color : UIColor, height : CGFloat) -> UIImage{
        
        let r = CGRect(x: 0, y: 0, width: 1, height: height)
        
        UIGraphicsBeginImageContext(r.size)
        
        let context = UIGraphicsGetCurrentContext()
        
        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, r)
        
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return img
    }
}